/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer.actions;

import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import org.heresylabs.rostercomposer.Composer;
import org.heresylabs.rostercomposer.formatter.CompactFormatter;
import org.heresylabs.rostercomposer.formatter.Formatter;
import org.heresylabs.rostercomposer.formatter.SimpleFormatter;
import org.heresylabs.rostercomposer.storage.RosterSaver;
import org.heresylabs.rostercomposer.storage.RosterStorage;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class SaveAction extends AbstractComposerAction {

    public SaveAction() {
        putValue(NAME, "Save");
        putValue(SMALL_ICON, new ImageIcon(getClass().getResource("/org/heresylabs/rostercomposer/images/document-save.png")));
        putValue(SHORT_DESCRIPTION, "Save current roster");
    }

    @Override
    protected void performAction(Composer composer) {
        FileFilter compactFile = new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().endsWith(".txt");
            }

            @Override
            public String getDescription() {
                return "*.txt (Compact text file)";
            }

        };

        FileFilter simpleFile = new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().endsWith(".txt");
            }

            @Override
            public String getDescription() {
                return "*.txt (Simple detailed text file)";
            }

        };

        FileFilter objectFile = new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().endsWith(".rstr");
            }

            @Override
            public String getDescription() {
                return "*.rstr (RosterComposer file for later usage)";
            }

        };

        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Save roster");
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setMultiSelectionEnabled(false);
        fc.setAcceptAllFileFilterUsed(false);
        fc.addChoosableFileFilter(compactFile);
        fc.addChoosableFileFilter(simpleFile);
        fc.addChoosableFileFilter(objectFile);
        int res = fc.showSaveDialog(null);
        if (res != JFileChooser.APPROVE_OPTION) {
            return;
        }
        File file = fc.getSelectedFile();
        if (file.exists()) {
            JOptionPane optionPane = new JOptionPane();
            optionPane.setMessage("File already exists. Are you sure you want to overwrite it?");
            optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
            optionPane.setOptionType(JOptionPane.YES_NO_OPTION);
            optionPane.setOptions(new String[] {"Yes", "No"});
            optionPane.setValue("No");
            JDialog dialog = optionPane.createDialog("Are you sure you want to overwrite file?");
            dialog.setVisible(true);
            // stupid thing that dialog is hidden but not disposed, that's why we can't exit from RosterComposer:
            dialog.dispose();
            if ("No".equals(optionPane.getValue())) {
                return;
            }
        }
        // if we are still here - file is OK

        FileFilter selectedFilter = fc.getFileFilter();

        if (selectedFilter == objectFile) {

            if (!file.getName().endsWith(".rstr")) {
                file = new File(file.getParentFile(), file.getName() + ".rstr");
            }

            RosterStorage storage = new RosterStorage();
            storage.setArmy(composer.getArmy());
            storage.setRoster(composer.getRoster());

            new RosterSaver().writeObject(file, storage);

        }
        else {

            if (!file.getName().endsWith(".txt")) {
                file = new File(file.getParentFile(), file.getName() + ".txt");
            }

            Formatter formatter = null;

            if (selectedFilter == compactFile) {
                formatter = new CompactFormatter(composer.getRoster());
            }
            else if (selectedFilter == simpleFile) {
                formatter = new SimpleFormatter(composer.getRoster());
            }

            if (formatter != null) {
                new RosterSaver().writeText(file, formatter);
            }
        }
    }

}
